home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2108 / 2108.xpi / components / stylishStartup.js < prev    next >
Text File  |  2009-05-26  |  7KB  |  197 lines

  1. function StylishStartup() {
  2. }
  3. StylishStartup.prototype = {
  4.     classID: Components.ID("{6ff9ed70-e673-11dc-95ff-0800200c9a66}"),
  5.     contractID: "@stylish/startup;2",
  6.     classDescription: "Stylish Startup",
  7.  
  8.     QueryInterface: function(aIID) {
  9.         if(!aIID.equals(CI.nsISupports) && !aIID.equals(CI.nsIObserver))
  10.             throw CR.NS_ERROR_NO_INTERFACE;
  11.         return this;
  12.     },
  13.  
  14.     observe: function(aSubject, aTopic, aData) {
  15.         switch(aTopic) {
  16.             case "xpcom-startup":
  17.                 var obsSvc = CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService);
  18.                 obsSvc.addObserver(this, "profile-after-change", false);
  19.                 break;
  20.  
  21.             case "profile-after-change":
  22.                 this.migrateFromRDF();
  23.                 var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle);
  24.                 service.findEnabled(true, service.REGISTER_STYLE_ON_LOAD, {});
  25.                 break;
  26.         
  27.         }
  28.     },
  29.  
  30.     migrateFromRDF: function() {
  31.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  32.  
  33.         //see if we actually have to do this
  34.         try {
  35.             if (prefs.getBoolPref("extensions.stylish.legacyFileMigrated")) {
  36.                 return;
  37.             }
  38.         } catch (ex) {
  39.             //pref doesn't exist
  40.         }
  41.  
  42.         //figure out where this is supposed to come from
  43.         var stream;
  44.         var prefPath = "";
  45.         try {
  46.             prefPath = prefs.getCharPref("extensions.stylish.fileURL");
  47.         } catch (ex) {
  48.             //pref doesn't exist
  49.         }
  50.         if (prefPath.length > 0) {
  51.             stream = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newChannel(prefPath, "UTF-8", null).open();
  52.         } else {
  53.             //go for the default location
  54.             var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
  55.             file.append("stylish.rdf");
  56.             if (!file.exists()) {
  57.                 return;
  58.             }
  59.             stream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
  60.             stream.init(file, -1, 0, 0);
  61.         }    
  62.  
  63.         //read in the file into a document
  64.         var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"].createInstance(Components.interfaces.nsIDOMParser);
  65.         var data = "";
  66.         var str = {};
  67.         var charset = "UTF-8";
  68.         const replacementChar = Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER;
  69.         var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream);
  70.         is.init(stream, charset, 1024, replacementChar);
  71.         while (is.readString(4096, str) != 0) {
  72.           data += str.value;
  73.         }
  74.         is.close();
  75.         stream.close();
  76.         //for some reason, parsing directly from the stream doesn't work
  77.         //var doc = parser.parseFromStream(stream, stream.available(), "UTF-8", "text/xml");
  78.         var doc = parser.parseFromString(data, "text/xml");
  79.  
  80.         //parse the document into new objects
  81.         const RDFNS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
  82.         const STYLISHNS = "urn:stylish#";
  83.         function getValue(e, ns, n) {
  84.             //it could be a child element or an attribute
  85.             var elements = e.getElementsByTagNameNS(ns, n);
  86.             if (elements.length > 0) {
  87.                 return elements[0].textContent;
  88.             }
  89.             return e.getAttributeNS(ns, n);
  90.         }
  91.         Array.forEach(doc.getElementsByTagNameNS(RDFNS, "Description"), function(e) {
  92.             var description = getValue(e, STYLISHNS, "description")
  93.             var code = getValue(e, STYLISHNS, "code");
  94.             if (!code || !description)
  95.                 return;
  96.             var url = getValue(e, RDFNS, "about");
  97.             if (/^rdf:/i.test(url)) {
  98.                 url = null;
  99.             }
  100.             var updateUrl = getValue(e, STYLISHNS, "updateURL")
  101.             var enabled = getValue(e, STYLISHNS, "enabled") == "true";
  102.             var style = Components.classes["@userstyles.org/style;1"].createInstance(Components.interfaces.stylishStyle);
  103.             style.mode = style.CALCULATE_META + style.REGISTER_STYLE_ON_LOAD;
  104.             style.init(url, updateUrl, null, description, code, enabled, null);
  105.             style.save();
  106.         });
  107.         prefs.setBoolPref("extensions.stylish.legacyFileMigrated", true);
  108.     }
  109.  
  110. };
  111.  
  112. var turnOnOffObserver = {
  113.     observe: function(subject, topic, data) {
  114.         var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle);
  115.         service.findEnabled(true, subject.QueryInterface(Components.interfaces.nsIPrefBranch2).getBoolPref(data) ? service.REGISTER_STYLE_ON_LOAD : service.UNREGISTER_STYLE_ON_LOAD, {});
  116.     }
  117. }
  118. Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).QueryInterface(Components.interfaces.nsIPrefBranch2).addObserver("extensions.stylish.styleRegistrationEnabled", turnOnOffObserver, false);
  119.  
  120.  
  121. // constructors for objects we want to XPCOMify
  122. var objects = [StylishStartup];
  123.  
  124. /*
  125.  * Registration code.
  126.  *
  127.  */
  128.  
  129. const CI = Components.interfaces, CC = Components.classes, CR = Components.results;
  130.  
  131. const MY_OBSERVER_NAME = "StylishStartup";
  132.  
  133. function FactoryHolder(aObj) {
  134.   this.CID        = aObj.prototype.classID;
  135.   this.contractID = aObj.prototype.contractID;
  136.   this.className  = aObj.prototype.classDescription;
  137.   this.factory = {
  138.     createInstance: function(aOuter, aIID) {
  139.       if(aOuter)
  140.         throw CR.NS_ERROR_NO_AGGREGATION;
  141.       return (new this.constructor).QueryInterface(aIID);
  142.     }
  143.   };
  144.   this.factory.constructor = aObj;
  145. }
  146.  
  147. var gModule = {
  148.   registerSelf: function (aComponentManager, aFileSpec, aLocation, aType)
  149.   {
  150.     aComponentManager.QueryInterface(CI.nsIComponentRegistrar);
  151.     for (var key in this._objects) {
  152.       var obj = this._objects[key];
  153.       aComponentManager.registerFactoryLocation(obj.CID, obj.className,
  154.         obj.contractID, aFileSpec, aLocation, aType);
  155.     }
  156.  
  157.     var catman = CC["@mozilla.org/categorymanager;1"].getService(CI.nsICategoryManager);
  158.     catman.addCategoryEntry("xpcom-startup", MY_OBSERVER_NAME,
  159.       StylishStartup.prototype.contractID, true, true);
  160.   },
  161.  
  162.   unregisterSelf: function(aCompMgr, aFileSpec, aLocation) {
  163.     var catman = CC["@mozilla.org/categorymanager;1"].getService(CI.nsICategoryManager);
  164.     catman.deleteCategoryEntry("xpcom-startup", MY_OBSERVER_NAME, true);
  165.  
  166.     aComponentManager.QueryInterface(CI.nsIComponentRegistrar);
  167.     for (var key in this._objects) {
  168.       var obj = this._objects[key];
  169.       aComponentManager.unregisterFactoryLocation(obj.CID, aFileSpec);
  170.     }
  171.   },
  172.  
  173.   getClassObject: function(aComponentManager, aCID, aIID) {
  174.     if (!aIID.equals(CI.nsIFactory)) throw CR.NS_ERROR_NOT_IMPLEMENTED;
  175.  
  176.     for (var key in this._objects) {
  177.       if (aCID.equals(this._objects[key].CID))
  178.         return this._objects[key].factory;
  179.     }
  180.    
  181.     throw CR.NS_ERROR_NO_INTERFACE;
  182.   },
  183.  
  184.   canUnload: function(aComponentManager) {
  185.     return true;
  186.   },
  187.  
  188.   _objects: {} //FactoryHolder
  189. };
  190.  
  191. function NSGetModule(compMgr, fileSpec)
  192. {
  193.   for(var i in objects)
  194.     gModule._objects[i] = new FactoryHolder(objects[i]);
  195.   return gModule;
  196.